//
//  GMHomeVC.m
//  StockSystem
//
//  Created by 小熊 on 2018/5/23.
//  Copyright © 2018年 小熊. All rights reserved.
//

#import "GMHomeVC.h"
#import "GMHomeTableViewCell.h"
#import "GMPlayerViewController.h"
#import "GMFirstTableViewCell.h"
#import "GMSecondViewCell.h"
#import "GMMyselfVC.h"
#import <AVFoundation/AVFoundation.h>
#import "QRCodeScanningVC.h"
#import "GMSearchResultsVC.h"
@interface GMHomeVC ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate,CellSelcetedDelegate,PYSearchViewControllerDelegate>
@property (nonatomic, strong) UITableView *tabView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) SDCycleScrollView *banarView;
@end

@implementation GMHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"腾云教育";
    [self addRightBarButtonWithFirstImage:[UIImage imageNamed:@"icon-NAV-scanner-"] action:@selector(sweepClick)];
    [self setUI];
}
- (void)sweepClick{
    // 1、 获取摄像设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (device) {
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (status == AVAuthorizationStatusNotDetermined) {
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                if (granted) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        QRCodeScanningVC *vc = [[QRCodeScanningVC alloc] init];
                        [self.navigationController pushViewController:vc animated:YES];
                    });
                    
                    SGQRCodeLog(@"当前线程 - - %@", [NSThread currentThread]);
                    // 用户第一次同意了访问相机权限
                    SGQRCodeLog(@"用户第一次同意了访问相机权限");
                    
                } else {
                    
                    // 用户第一次拒绝了访问相机权限
                    SGQRCodeLog(@"用户第一次拒绝了访问相机权限");
                }
            }];
        } else if (status == AVAuthorizationStatusAuthorized) { // 用户允许当前应用访问相机
            QRCodeScanningVC *vc = [[QRCodeScanningVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        } else if (status == AVAuthorizationStatusDenied) { // 用户拒绝当前应用访问相机
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"⚠️ 警告" message:@"请去-> [设置 - 隐私 - 相机 - SGQRCodeExample] 打开访问开关" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alertC addAction:alertA];
            [self presentViewController:alertC animated:YES completion:nil];
            
        } else if (status == AVAuthorizationStatusRestricted) {
            NSLog(@"因为系统原因, 无法访问相册");
        }
    } else {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"未检测到您的摄像头" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertC addAction:alertA];
        [self presentViewController:alertC animated:YES completion:nil];
    }

}
- (void)setUI{
    [self.tabView registerNib:[UINib nibWithNibName:@"GMSecondViewCell" bundle:nil] forCellReuseIdentifier:@"GMSecondViewCell"];
    [self.tabView registerNib:[UINib nibWithNibName:@"GMFirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"GMFirstTableViewCell"];
    self.tabView.tableHeaderView = self.banarView;
}
- (SDCycleScrollView *)banarView{
    if (!_banarView) {
        _banarView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 180) delegate:self placeholderImage:DefealtImg];
        _banarView.bannerImageViewContentMode = UIViewContentModeScaleToFill;
        _banarView.localizationImageNamesGroup = @[@"guide_01",@"guide_02",@"guide_03",@"guide_04"];
        _banarView.titlesGroup =@[@"标题1",@"标题2",@"标题3",@"标题4"];
        _banarView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _banarView.autoScrollTimeInterval = 5;
    }
    return _banarView;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (UITableView *)tabView{
    if (!_tabView) {
        _tabView =[UITableView zj_tableViewWithSuperview:self.view delegate:self style:UITableViewStylePlain constraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        [_tabView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        _tabView.showsVerticalScrollIndicator = NO;
    }
     return _tabView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        GMFirstTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"GMFirstTableViewCell" forIndexPath:indexPath];
        return cell;
    }else{
        GMSecondViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"GMSecondViewCell" forIndexPath:indexPath];
        cell.delegate =self;
        return cell;
    }
  
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 170;
    }else{
         return 450;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
#pragma cell的点击方法
- (void)cellSelectedWithIndex:(NSInteger)index {
    GMPlayerViewController *player =[[GMPlayerViewController alloc] init];
    [self.navigationController pushViewController:player animated:YES];
}
- (void)searchBtnClick{
    // 1. Create an Array of popular search
    NSArray *hotSeaches = @[@"Java", @"Python", @"Objective-C", @"Swift", @"C", @"C++", @"PHP", @"C#", @"Perl", @"Go", @"JavaScript", @"R", @"Ruby", @"MATLAB"];
    // 2. Create a search view controller
    PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:@"请输入关键词" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
    
        [searchViewController.navigationController pushViewController:[[GMSearchResultsVC alloc] init] animated:YES];
    }];

    searchViewController.hotSearchStyle = PYHotSearchStyleDefault;
    searchViewController.searchHistoryStyle = PYSearchHistoryStyleNormalTag;
    // 4. Set delegate
    searchViewController.delegate = self;
    searchViewController.searchViewControllerShowMode = PYSearchViewControllerShowModePush;
    //    // Push search view controller
    [self.navigationController pushViewController:searchViewController animated:YES];
}

@end
